import { toSuccess, useTransaction } from "@sker/core"
import { SysMenu } from "@sker/entities"
import { Like } from "typeorm"

export default async () => {
    await useTransaction(async m => {
        // 检查是否有s3菜单
        const menus = await m.find(SysMenu, { where: { name: Like('ImeeposCore%') } })
        if (menus.length > 0) {
            await m.delete(SysMenu, menus.map(it => it.id))
        }
        const addonMenus: any[] = [
            { name: 'ImeeposCore', title: '系统管理', url: null, main: null, is_group: true },
            { name: 'ImeeposCoreWebIndex', title: '系统概述', pname: 'ImeeposCore', url: '/imeepos/core/web/index', main: '/@imeepos/core/public/web/index.js', is_group: false },
            { name: 'ImeeposCoreWebAddon', title: '应用管理', pname: 'ImeeposCore', url: '/imeepos/core/web/addon', main: '/@imeepos/core/public/web/addon.js', is_group: false },
            { name: 'ImeeposCoreWebAttachment', title: '附件管理', pname: 'ImeeposCore', url: '/imeepos/core/web/attachment', main: '/@imeepos/core/public/web/attachment.js', is_group: false },
            { name: 'ImeeposCoreWebCode', title: '系统字典', pname: 'ImeeposCore', url: '/imeepos/core/web/code', main: '/@imeepos/core/public/web/code.js', is_group: false },
            { name: 'ImeeposCoreWebIcon', title: '图标管理', pname: 'ImeeposCore', url: '/imeepos/core/web/icon', main: '/@imeepos/core/public/web/icon.js', is_group: false },
            { name: 'ImeeposCoreWebMenu', title: '菜单管理', pname: 'ImeeposCore', url: '/imeepos/core/web/menu', main: '/@imeepos/core/public/web/menu.js', is_group: false },
            { name: 'ImeeposCoreWebPlugin', title: '插件管理', pname: 'ImeeposCore', url: '/imeepos/core/web/plugin', main: '/@imeepos/core/public/web/plugin.js', is_group: false },
            { name: 'ImeeposCoreWebRole', title: '角色管理', pname: 'ImeeposCore', url: '/imeepos/core/web/role', main: '/@imeepos/core/public/web/role.js', is_group: false },
            { name: 'ImeeposCoreWebTheme', title: '主题管理', pname: 'ImeeposCore', url: '/imeepos/core/web/theme', main: '/@imeepos/core/public/web/theme.js', is_group: false },
            { name: 'ImeeposCoreWebUser', title: '用户管理', pname: 'ImeeposCore', url: '/imeepos/core/web/user', main: '/@imeepos/core/public/web/user.js', is_group: false },
            { name: 'ImeeposCoreWebSetting', title: '站点设置', pname: 'ImeeposCore', url: '/imeepos/core/web/setting', main: '/@imeepos/core/public/web/setting.js', is_group: false },
        ]
        const toSave = addonMenus.map(menu => {
            const item = menus.find(it => it.name === menu.name)
            if (item) {
                menu.id = item.id;
            }
            return menu;
        })
        await m.save(SysMenu, toSave)
    })
    return toSuccess(true)
}